<template>
<div class="login_container">
<!--登录盒子区域-->
  <div class="login_box">
    <!--头像区域-->
      <div class="avatar_box">
        <img src="../assets/QQ图片20210824170350.jpg" alt="">
      </div>
    <!--登录表单区域-->
    <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
      <!--用户名-->
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
      </el-form-item>
      <!--密码-->
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
      </el-form-item>
      <!--滑块验证区域-->
      <el-form-item>
        <el-row>
          <drag-verify
            :circle="false"
            ref="dragVerify"
            text="请按住滑块拖动"
            successText="验证通过"
            handlerIcon="el-icon-d-arrow-right"
            successIcon="el-icon-circle-check"
            :width="410"
            :height="45"
            @passcallback="passCallback">
          </drag-verify>
        </el-row>
      </el-form-item>
      <!--按钮区域-->
      <el-form-item class="btns" >
       <el-button type="primary" @click="login">登录</el-button>
       <el-button type="info" @click="resetLoginForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
  import dragVerify from 'vue-drag-verify'
  export default {
    data(){
      return{
        //登录表单数据对象
        loginForm:{
          username:'admin',
          password:'123456'
        },
        loginFormRules:{
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
          ]
        }

      }
    },
    components:{
      dragVerify
    },
    methods:{
      //重置
      resetLoginForm(){
        this.$refs.loginFormRef.resetFields()
      },
      //登录
      login(){
        //发送请求前先进行预校验
        this.$refs.loginFormRef.validate(async valid => {
          if (!valid||!this.$refs.dragVerify.isPassing) {
            return this.$message.warning('请检查表单项并滑动滑块')
          }

         const {data: res }= await this.$http.post('/login', this.loginForm)

          if (res.meta.status!==200){
            return this.$message({
              message:'登录失败！请检查用户名和密码是否正确~',
              type:'error',
              duration:1500
            })
          }
          this.$message({
            message:`欢迎${this.loginForm.username}登录(✪ω✪)`,
            type:'success'
          })
          window.sessionStorage.setItem('token',res.data.token)

          //通过编程式路由导航跳到主页
          this.$router.push('/home')

        })
      },
      //滑块通过后的回调
      passCallback(){
        this.$message({
          message:'验证通过',
          type:'success',
          duration:800
        })
      }
    }
  }
</script>

<style scoped lang="less">
.login_container{
  background: url("../assets/1.jpg");
  height: 100%;
  background-size: 100% 100%;
}
//头像区域样式
  .login_box{
    width: 450px;
    height: 370px;
    background-color: white;
    opacity: 0.8;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);

    .avatar_box{
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 1px solid #eeeeee;
      position: absolute;
      left: 50%;
      transform: translate(-50%,-50%);
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
  }

.login_form{
  box-sizing: border-box;
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
}
  .btns{
    display: flex;
    justify-content: flex-end;
  }
</style>
